﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;

namespace SQLInstanceMemoryCalculator
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void lblThreadStaclSize_Click(object sender, EventArgs e)
        {

        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            double totalram = 0;
            int corenumber = 0;
            int architecutre = 0;
            double otherapps = 0;
            double threadstacksize = 0;
            double forOS = 0;
            double SQLThreads = 0;
            double MaxMemory = 0;
            int coretemp = 0;
            int gbormb = drpMB.SelectedIndex;

            try
            {

                totalram = Double.Parse(txtTotalRam.Text.ToString());
                corenumber = int.Parse(txtCores.Text.ToString());
                architecutre = drpArchitecture.SelectedIndex;
                otherapps = Double.Parse(txtOtherApps.Text.ToString());
            }
            catch
            {
                MessageBox.Show("You entered Wrong Data. Please check it and try again");
                return;
            }

        // Find ThreadStackSize 1MB x86/ 2MB x64/ 4MB IA64
            switch (architecutre)
            {
                case 0:
                    threadstacksize = 1;
                    break;
                case 1:
                    threadstacksize = 2;
                    break;
                case 2:
                    threadstacksize = 4;
                    break;
            }

            if (gbormb == 1)
            {

                totalram = totalram * 1024;
            }

        
        //FIND how much to save for OS.  20% of total ram for under 15GB  12.5% for over 20GB

            if (totalram > 20480)
            {
                forOS = 0.125 * totalram;
            }
            else
            {
                forOS = 0.2 * totalram;
            }
        
        //find Number of SQL Threads = 256 + (NumofProcesors - 4) * 8
            if (corenumber > 4)
            {
                coretemp = 0;
            }
            else
            {
                coretemp = corenumber;
            }

            SQLThreads = 256 + (coretemp - 4) * 8;

            double temp = corenumber / 4;

            otherapps = double.Parse(txtOtherApps.Text.ToString());

            MaxMemory = (totalram - forOS - otherapps - (SQLThreads * threadstacksize) - (1024 * Math.Ceiling(temp)));


            lblOS.Text = "OS Reserved = " + forOS + "MB";
            lblThreadStaclSize.Text = "Thread Stack Size = " + threadstacksize;
            lblSqlThreads.Text = "Number of SQL Threads= " + SQLThreads;
            txtMax.Text = MaxMemory + " MB ";

            btnHow.Visible = true;

        }

        private void lblOS_Click(object sender, EventArgs e)
        {

        }

        private void Main_Load(object sender, EventArgs e)
        {
            drpArchitecture.SelectedIndex = 1;
            drpMB.SelectedIndex = 0;
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void btnHow_Click(object sender, EventArgs e)
        {

            lblMagic.Visible = true;
            lblFormula.Visible = true;
            lblOS.Visible = true;
            lblThreadStaclSize.Visible = true;
            lblSqlThreads.Visible = true;

        }


    }
}
